package com.eric.service.impl;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.eric.domain.dto.CompanyInfoDTO;
import com.eric.domain.po.CompanyInfo;
import com.eric.domain.vo.CompanyInfoVO;
import com.eric.mapper.CompanyInfoMapper;
import com.eric.service.CompanyInfoService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.eric.domain.po.table.CompanyInfoTableDef.COMPANY_INFO;

/**
 *  服务层实现。
 *
 * @author eric.deng
 * @since 2025-03-05
 */
@Service
public class CompanyInfoServiceImpl extends ServiceImpl<CompanyInfoMapper, CompanyInfo>  implements CompanyInfoService{

    @Override
    public SaResult saveOneService(CompanyInfoVO companyInfoVO) {
        CompanyInfo one = queryChain().select()
                .where(COMPANY_INFO.COMPANY_NAME.eq(companyInfoVO.getCompanyName()))
                .one();
        if (BeanUtil.isEmpty(one)){
            CompanyInfo companyInfo=new CompanyInfo();
            BeanUtil.copyProperties(companyInfoVO,companyInfo);
            boolean save=save(companyInfo);
            return save?SaResult.ok():(SaResult.error("录入错误"));
        }else {
            return  SaResult.error("信息已存在");
        }
    }

    /*
    *  private Integer id;

    @NotBlank(message = "公司名不能为空")
    private String companyName;

    @NotBlank(message = "公司信息不能为空")
    private String companyInfo;

    private String createTime;

    private String updateTime;

    private String description;
    *
    * */

    @Override
    public SaResult updateByIdService(CompanyInfoDTO companyInfoDTO) {
        boolean companyNameEffective=false;
        boolean companyInfoEffective=false;
        boolean descriptionEffective=false;
        if (!StrUtil.isBlank(companyInfoDTO.getCompanyName())){
            companyNameEffective=true;
        }
        if (!StrUtil.isBlank(companyInfoDTO.getCompanyInfo())){
            companyInfoEffective=true;
        }
        if (!StrUtil.isBlank(companyInfoDTO.getDescription())){
            descriptionEffective=true;
        }
        CompanyInfo companyInfo = queryChain()
                .where(COMPANY_INFO.COMPANY_NAME.eq(companyInfoDTO.getCompanyName()))
                .one();
        if (!BeanUtil.isEmpty(companyInfo)&& !Objects.equals(companyInfo.getId(),companyInfoDTO.getId())){
            return SaResult.error("信息已经存在！");
        }

        boolean updated = UpdateChain.of(CompanyInfo.class)
                .set(COMPANY_INFO.COMPANY_NAME, companyInfoDTO.getCompanyName(), companyNameEffective)
                .set(COMPANY_INFO.COMPANY_INFO_, companyInfoDTO.getCompanyInfo(), companyInfoEffective)
                .set(COMPANY_INFO.DESCRIPTION, companyInfoDTO.getDescription(), descriptionEffective)
                .where(COMPANY_INFO.ID.eq(companyInfoDTO.getId()))
                .update();
        return SaResult.data(updated);


    }

    @Override
    public SaResult getListByPageService(Integer cur, Integer pageSize) {
        if (cur==null||cur<=0){
            cur=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=11;
        }
        List<CompanyInfo> companyInfoList = queryChain().select()
               .from(COMPANY_INFO).orderBy(COMPANY_INFO.UPDATE_TIME.desc())
               .page(new Page<>(cur, pageSize)).getRecords();
        if (!companyInfoList.isEmpty()){
            List<CompanyInfoDTO> companyInfoDTOList = BeanUtil.copyToList(companyInfoList, CompanyInfoDTO.class);
            return SaResult.data(companyInfoDTOList);
        }
        return SaResult.error("没有数据");
    }

    @Override
    public SaResult getCompanyInfoListByNameService(String name, Integer cur, Integer pageSize) {
        if (cur==null||cur<=0){
            cur=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=11;
        }
        List<CompanyInfo> companyInfoList = queryChain().select()
                .where(COMPANY_INFO.COMPANY_NAME.like(name, true))
                .orderBy(COMPANY_INFO.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize)).getRecords();
        if (!companyInfoList.isEmpty()){
            List<CompanyInfoDTO> companyInfoDTOList = BeanUtil.copyToList(companyInfoList, CompanyInfoDTO.class);
            return SaResult.data(companyInfoDTOList);
        }
        return SaResult.error("没有数据");
    }

    @Override
    public SaResult getCompanyInfoListByNameCountService(String name) {
        long count = queryChain().select()
                .where(COMPANY_INFO.COMPANY_NAME.like(name, true))
                .count();
        return SaResult.data(count);
    }
}
